﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Microsoft.WowAddonStudio.FrameXml.Serialization.Attributes;
using Microsoft.WowAddonStudio.FrameXml.Serialization.Enumerations;
using Microsoft.WowAddonStudio.FrameXml.Serialization.LayoutFrames;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Editors
{
    public class TextureEditor : TreePickerEditor
    {
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"/> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"/> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var texture = value != null ? ((Texture)value).Copy() : new Texture();

            if (provider != null)
            {
                if (context.PropertyDescriptor.Attributes != null)
                {
                    ReadOnlyAttribute attribute = context.PropertyDescriptor.Attributes.OfType<ReadOnlyAttribute>().FirstOrDefault();
                    if (attribute != null)
                    {
                        if (attribute.IsReadOnly) return value;
                    }
                }

                CreateResourceTree(context, provider);

                var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                FileType currentFileType = ExtractFileType(context);
                Type editorFormType = typeof(TreePickerEditorForm);

                if (extensionEditorFormType.ContainsKey(currentFileType))
                    editorFormType = extensionEditorFormType[currentFileType];

                using (var editorForm = (TreePickerEditorForm)Activator.CreateInstance(editorFormType, element, provider))
                {
                    editorForm.RelativePath = texture.File;
                    if (editorService.ShowDialog(editorForm) == DialogResult.OK)
                    {
                        if (texture.File != editorForm.RelativePath)
                        {
                            texture.File = editorForm.RelativePath;
                            return texture;
                        }
                    }
                }
            }

            return value;
        }

        /// <summary>
        /// Extracts the extension.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        protected override FileType ExtractFileType(ITypeDescriptorContext context)
        {
            var attribute = context.PropertyDescriptor.Attributes.OfType<TextureAttribute>().FirstOrDefault();

            if (attribute != null)
                return attribute.FileType;

            return FileType.Unknown;
        }
    }
}
